package com.community.app.controller;

import com.community.body.domain.ZyFiles;
import com.community.body.domain.ZyRepair;
import com.community.body.dto.ServicemanDTO;
import com.community.body.service.IZyFilesService;
import com.community.body.service.IZyRepairService;
import com.community.common.core.domain.AjaxResult;
import com.community.common.utils.file.FileUtils;
import com.community.common.utils.oss.AliyunOssUploadUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mini/community/wx/fix")
public class WxRepairController {

    @Resource
    private IZyRepairService zyRepairService;
    @Resource
    private RedisTemplate<String, Long> redisTemplate;
    @Resource
    private IZyFilesService zyFilesService;

    /**
     * 查询报修信息列表
     */
    @GetMapping("selectFix")
    public AjaxResult list(ServicemanDTO servicemanDTO) {
        List<ServicemanDTO> list = zyRepairService.selectZyRepairList(servicemanDTO);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询报修信息
     */
    @GetMapping("repairId")
    public AjaxResult getInfo(ServicemanDTO servicemanDTO) {
        return AjaxResult.success(zyRepairService.selectZyRepairList(servicemanDTO));
    }

    /**
     * 根据id删除报修信息
     */
    @GetMapping("delRepair")
    public AjaxResult delRepair(ServicemanDTO servicemanDTO) {
        int i = zyRepairService.deleteZyRepairByRepairId(servicemanDTO.getRepairId());
        return i > 0 ? AjaxResult.success("删除成功！") : AjaxResult.error("删除失败！");
    }

    /**
     * 新增报修信息
     */
    @PostMapping("insertFix")
    public AjaxResult add(@RequestBody ZyRepair zyRepair) {
        int i = zyRepairService.insertZyRepair(zyRepair);
        return i > 0 ? AjaxResult.success("添加成功！") : AjaxResult.error("添加失败！");
    }

    /**
     * 报修图片上传
     *
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("fixUpload")
    public AjaxResult fixUpload(MultipartFile file, HttpServletRequest request) throws Exception {
        Long owner = Long.valueOf(request.getHeader("owner"));
        if (!file.isEmpty()) {
            String url = AliyunOssUploadUtils.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", FileUtils.getName(url));
            ajax.put("newFileName", FileUtils.getName(url));
            ajax.put("originalFilename", file.getOriginalFilename());
            ZyFiles zyFiles = new ZyFiles();
            zyFiles.setFilesUrl(url);
            zyFiles.setDelFlag(0L);
            zyFiles.setCreateTime(new Date());
            zyFiles.setUserId(owner);
            zyFiles.setSource(0L);
            ValueOperations<String, Long> value = redisTemplate.opsForValue();
            Long s = value.get("repairId");
            zyFiles.setParentId(s);
            zyFiles.setRemark("repair");
            zyFilesService.insertZyFiles(zyFiles);
            return AjaxResult.success("上传成功", ajax);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }


    /**
     * 报修撤销  && 报修完成
     * @param servicemanDTO
     * @return
     */
    @PostMapping("cancelRepair")
    public AjaxResult cancelRepair(@RequestBody ServicemanDTO servicemanDTO){
        int i = zyRepairService.updateZyRepair(servicemanDTO);
        return i > 0 ? AjaxResult.success("撤销成功！") : AjaxResult.error("撤销失败！");
    }
}
